package com.gy.mbaselibrary.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.gy.mbaselibrary.net.BaseRequest;
import com.gy.mbaselibrary.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * 创建者：高业
 * 邮箱：15234150168@163.com
 * 时间：2018/1/31
 * 类描述：基类activity,项目中所有的actiivty都继承自此类
 * 修改人：
 * 修改时间：2018/1/31
 * 修改备注：
 */
@SuppressLint("WrongConstant")
public abstract class BaseActivity extends AppCompatActivity implements BaseRequest.ActivityResponseListener{
    //用来调用分页接口,区分是刷新还是加载
    protected final String TAG_LOAD = "load";
    protected final String TAG_REFRESH = "refresh";
    //当前页,每页显示的条数
    protected int currentPage = 1;
    //每页显示个数
    protected int pageSize = 20;
    //当前的fragment
    private Fragment currentFragment;
    private FragmentManager fragmentManager;
    //当前的上下文对象
    private Context currentContext;
    //获取上下文对象
    public Context getCurrentContext() {
        return currentContext;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        currentContext = this;
        /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            ScreenUtil.setWindowStatusBarColor(this, Color.parseColor("#337FFD"));
            setLightMode(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }else {
            ScreenUtil.setWindowStatusBarColor(this, Color.parseColor("#000000"));
        }*/
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);

        intScreen();
        if (setContentView() != -1){
            setContentView(setContentView());
        }
        initUser();
        //int width = ScreenUtil.getScreenWidth(this);
        //int height = ScreenUtil.getScreenHeight(this);
        //int bottomStatusHeight = ScreenUtil.getBottomStatusHeight(this);
        //LogUtil.i("屏幕高度:" + ScreenUtil.getScreenHeight(this) + "屏幕宽度:" + ScreenUtil.getScreenWidth(this));
        ButterKnife.bind(this);

        initTitle();
        initView();
        initData(savedInstanceState);
        initListener();
    }



    /**
     * 设置屏幕
     */
    protected abstract void intScreen();
    /**
     * 设置布局文件
     * @return
     */
    protected abstract int setContentView();
    /**
     * 设置title
     */
    protected abstract void initTitle();

    /**
     * 设置view
     */
    protected abstract void initView();

    /**
     * 设置数据
     */
    protected abstract void initData(Bundle savedInstanceState);

    /**
     * 设置监听
     */
    protected abstract void initListener();
    /**
     * 设置用户
     */
    protected abstract void initUser();


    //跳转页面的基础方法
    public void goToBase(Class clz, final Bundle bundle, final int requestCode, final boolean isFinish){
                    Intent intent = new Intent(this,clz);
                    //设置bundle
                    if (bundle != null){
                        intent.putExtras(bundle);
                    }
                    //设置requestcode
                    startActivityForResult(intent,requestCode);
        //动画效果
        //overridePendingTransition(R.anim.activity_open_common, 0);
        //设置是否结束当前页面
        if (isFinish){
            finish();
        }
    }

    //跳转页面
    public void goTo(Class clz){
        goTo(clz,null);
    }
    //跳转页面
    public void goTo(Class clz, Bundle bundle){
        goToBase(clz,bundle,-1,false);
    }
    //跳转页面
    public void goTo(Class clz, int requestCode){
        goToBase(clz,null,requestCode,false);
    }

    //跳转页面
    public void goTo(Class clz, int requestCode, Bundle bundle){
        goToBase(clz,bundle,requestCode,false);
    }

    //跳转页面并销毁当前页面
    public void goToAndFinish(final Class clz){
        goToBase(clz,null,-1,true);
    }

    //跳转页面并销毁当前页面
    public void goToAndFinish(final Class clz,Bundle bundle){
        goToBase(clz,bundle,-1,true);
    }


    @Override
    public void parseNetworkResponse(int requestCode,String url, List<String> list) {
    }

    public List<View> getAllChildViews() {
        View view = this.getWindow().getDecorView();
        return getAllChildViews(view);
    }

    //获取子view
    private List<View> getAllChildViews(View view) {
        List<View> allchildren = new ArrayList<View>();
        if (view instanceof ViewGroup) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View viewchild = vp.getChildAt(i);
                allchildren.add(viewchild);
                allchildren.addAll(getAllChildViews(viewchild));
            }
        }
        return allchildren;
    }


    /**
     * SYSTEM_UI_FLAG_LAYOUT_STABLE  字体白色
     * SYSTEM_UI_FLAG_LIGHT_STATUS_BAR  字体黑色
     * */
    protected void setLightMode(int statue) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 设置状态栏字体黑色
            getWindow().getDecorView().setSystemUiVisibility(statue);
        }
    }

}
